/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package forms;

import com.sun.lwuit.Command;
import com.sun.lwuit.List;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.plaf.Border;
import domain.controller.file.CategoriaController;
import domain.model.ICategoria;
import domain.model.file.Categoria;
import form.components.CategoriaListRenderer;

/**
 *
 * @author ademar.victorino
 */
public class CategoriaForm extends AbstractForm implements ActionListener {

    private ICategoria catCorrente = null;
    private List list = new List();
    private CategoriasRaizForm returnForm;

    public CategoriaForm() {
        super();
        setLayout(new BoxLayout(BoxLayout.Y_AXIS));
        list.setListCellRenderer(new CategoriaListRenderer());
        addComponent(list);
        //list.setFocusable(true); list.setFocus(true); list.setFocusPainted(true);list.requestFocus(); setFocused(list);
        list.setFixedSelection(List.FIXED_LEAD);
        list.getStyle().setBorder(Border.createEmpty());
        list.addActionListener(this);

        addCommand(new Command("Voltar") {

            public void actionPerformed(ActionEvent evt) {
                voltarAction();
            }
        });
    }

    public void show(Categoria catCorrente, CategoriasRaizForm returnForm) {
        this.catCorrente = catCorrente;
        this.returnForm = returnForm;
        show();
    }

    public void show() {
        init();
        super.show();
    }

    private void init() {
        try {
            setTitle(catCorrente.getNome());
            list.setModel(new DefaultListModel(CategoriaController.getInstance().listSubCategoria(catCorrente)));

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void voltarAction() {

        try {

            if (catCorrente.getCategoriaPaiId() == -1) {
                returnForm.show();

            } else {
                catCorrente = CategoriaController.getInstance().getCategoria(catCorrente.getCategoriaPaiId());
                init();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void goToNextForm() {
        ICategoria catSel = (ICategoria) list.getSelectedItem();
        if (catSel.getCategoriasFilhasIds().size() > 0) {
            this.catCorrente = catSel;
            init();
            WaitForm.getInstance().setVisible(false);
            this.show();
        } else {
            new RegistrosForm(this).show(catSel);
        }
    }

    public void actionPerformed(ActionEvent arg0) {
        WaitForm.getInstance().show();
        new Thread(new Runnable() {

            public void run() {
                goToNextForm();
            }
        }).start();
    }
    /*
    public Vector sortByLabelAlpha(Vector elements) {
    Vector returnVal = new Vector(elements.size());
    for (int a = 0; a < elements.size(); a++) {
    returnVal.addElement(new Categoria());
    }
    final int vectorSize = elements.size();

    for (int i = 1; i < vectorSize; i++) {
    final Categoria mObj = (Categoria) elements.elementAt(i);
    int j = i - 1;
    for (; j >= 0; j--) {
    Categoria r = (Categoria) elements.elementAt(j);
    if (r.nome.compareTo(mObj.nome) <= 0) {
    break;
    }
    returnVal.removeElementAt(j + 1);
    returnVal.insertElementAt(r, j + 1);
    }
    returnVal.removeElementAt(j + 1);
    returnVal.insertElementAt(mObj, j + 1);
    }
    return returnVal;
    }*/
}
